unit uSAF0501_03;

interface

uses
   Windows,
   Messages,
   SysUtils,
   Variants,
   Classes,
   Graphics,
   Controls,
   Forms,
   Dialogs,
   ComCtrls,
   ToolWin,
   Menus,
   ActnList,
   ImgList,
   ExtCtrls,
   DB,
   ADODB,
   DBCtrls,
   StdCtrls,
   WideStrings,
   FMTBcd,
   Mask,
   Buttons,
   rxToolEdit,
   RXDBCtrl;

type
   TfrmInsUpd = class(TForm)
      tlbInsUpd: TToolBar;
      btnSalvar: TToolButton;
      btnS1: TToolButton;
      btnFechar: TToolButton;
      statPrincipal: TStatusBar;
      pnlCentro: TPanel;
      actInsUpd: TActionList;
      actInsUpdSalvar: TAction;
      actInsUpdFechar: TAction;
      ilInsUpd: TImageList;
      dsTabela: TDataSource;
      lblID_OC: TLabel;
      btnID_OC: TBitBtn;
      lblCONTRATO_DATA: TLabel;
      edtDATA: TDBDateEdit;
      lblTARA_CAIXA: TLabel;
      edtTARA_CAIXA: TDBEdit;
      lblTOTAL_QTDE: TLabel;
      edtTOTAL_QTDE: TDBEdit;
      lblTOTAL_PESO_LIQUIDO: TLabel;
      edtTOTAL_PESO_LIQUIDO: TDBEdit;
      lblID_CONTRATO_REF_NRO: TLabel;
      txtID_PRODUTOR_FANTASIA: TDBText;
      txtEMBARQUE_SEMANA: TDBText;
      lbl2: TLabel;
      txtEMBARQUE_SEMANA_LIMITE: TDBText;
      lblID_LOCAL_EMBARQUE_DSC: TLabel;
      txtID_LOCAL_EMBARQUE_DSC: TDBText;
      lblID_LOCAL_ENTREGA_DSC: TLabel;
      txtID_LOCAL_ENTREGA_DSC: TDBText;
      cbbID_OC: TDBLookupComboBox;
      edtSIF_NRO: TDBEdit;
      lblSIF_NRO: TLabel;

      procedure FormShow(Sender: TObject);

      procedure FormKeyPress(Sender: TObject; var Key: Char);
      procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);

      procedure actInsUpdSalvarExecute(Sender: TObject);
      procedure actInsUpdFecharExecute(Sender: TObject);
      procedure dsTabelaStateChange(Sender: TObject);
      procedure btnID_OCClick(Sender: TObject);

   private

      { Private declarations }

   public

      _INSUPD_TIPO: Integer;
      _INSUPD_ID: Integer;

      { Public declarations }

   const

      {$REGION 'CONSTANTES CHAVES DA BUSCA'}
      _BUSCA_01_OC = 1;
      {$ENDREGION}

   end;

var
   frmInsUpd: TfrmInsUpd;

implementation

{$R *.dfm}

uses
   uLibGlobal,
   uSAF0501_01,
   uSAF0501_02,
   uSAF0501_04,
   uSAF0501_05;

procedure TfrmInsUpd.actInsUpdFecharExecute(Sender: TObject);
begin
   if (DMDados.tblTabela.State in [dsInsert, dsEdit]) then
   begin
      DMDados.tblTabela.Cancel;
   end;

   Self.Close;
end;

procedure TfrmInsUpd.actInsUpdSalvarExecute(Sender: TObject);
var
   chaveID: integer;
   tipoPessoa: Integer;
   codigoEmpresa: Integer;
   nroCPF: string;
   nroCNPJ: string;
   sAux: string;
   codigoPais: Integer;
begin
   (*
   {$REGION 'TRATA ID_EMPRESA'}
   //if DMDados.tblTabela.FieldByName('ID_EMPRESA').IsNull then
   if (codigoEmpresa <= 0) then
   begin
      Application.MessageBox(PChar(
         'O CAMPO: ' + #13 +
         'EMPRESA' + #13 +
         'ESTA EM BRANCO.'
         ), PChar(Application.Title), MB_OK +
         MB_ICONINFORMATION);

      Abort;
   end;
   {$ENDREGION}
   *)

   if (DMDados.tblTabela.State in [dsInsert, dsEdit]) then
   begin
      if Application.MessageBox(
         PChar('DESEJA REALMENTE SALVAR O REGISTRO ?'),
         PChar(Application.Title),
         MB_YESNO + MB_ICONQUESTION) = IDYES then
      begin
         DMDados.tblTabela.Post;

         fAtualizaDS(DMDados.qryLista);

         actInsUpdFecharExecute(Sender);
      end;
   end;
end;

procedure TfrmInsUpd.btnID_OCClick(Sender: TObject);
var
   retornoBusca: Integer;
begin
   try
      FreeAndNil(frmBuscar);
   finally
      frmBuscar := TfrmBuscar.Create(nil);
      frmBuscar._TIPO_BUSCA := _BUSCA_01_OC;
      frmBuscar.ShowModal;

      retornoBusca := frmBuscar._RETORNA_BUSCA;

      if (retornoBusca > 0) then
      begin
         DMDados.tblTabela.FieldByName('ID_OC').Value := retornoBusca;
      end;

      cbbID_OC.SetFocus;
   end;
end;

procedure TfrmInsUpd.dsTabelaStateChange(Sender: TObject);
begin
   cbbID_OC.Enabled := (dsTabela.State in [dsInsert, dsEdit]);
   btnID_OC.Enabled := (dsTabela.State in [dsInsert, dsEdit]);

   edtDATA.Enabled := (dsTabela.State in [dsInsert, dsEdit]);
   edtTARA_CAIXA.Enabled := (dsTabela.State in [dsInsert, dsEdit]);
   edtTOTAL_QTDE.Enabled := (dsTabela.State in [dsInsert, dsEdit]);
   edtTOTAL_PESO_LIQUIDO.Enabled := (dsTabela.State in [dsInsert, dsEdit]);
   edtSIF_NRO.Enabled := (dsTabela.State in [dsInsert, dsEdit]);

   actInsUpdSalvar.Enabled := (dsTabela.State in [dsInsert, dsEdit]);
end;

procedure TfrmInsUpd.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
   fNotCtrlDelete(Key, Shift);
end;

procedure TfrmInsUpd.FormKeyPress(Sender: TObject; var Key: Char);
begin
   fTabToEnter(Key, Self);
end;

procedure TfrmInsUpd.FormShow(Sender: TObject);
begin
   fMontarTelaInsUpd(Self, 310, 500);

   //CADASTRO DE PRODUTOR
   fAtualizaDS(DMDados.tblSAF0102_PRODUTOR);

   //CADASTRO DE ORDEM DE COMPRA
   fAtualizaDS(DMDados.tblSAF0301);

   //TABELA
   fAtualizaDS(DMDados.tblTabela);

   case _INSUPD_TIPO of
      0: //INSERIR
         begin
            Self.Caption := DMDados._sistemaNome + ' - INSERIR REGISTRO';
            DMDados.tblTabela.Insert;
         end;

      1: //EDITAR
         begin
            Self.Caption := DMDados._sistemaNome + ' - ALTERAR REGISTRO';
            DMDados.tblTabela.Locate(
               DMDados._TIPO_CAMPO,
               _INSUPD_ID, [loCaseInsensitive, loPartialKey]);

            DMDados.tblTabela.Edit;
         end;

      2: //CONSULTAR
         begin
            Self.Caption := DMDados._sistemaNome + ' - CONSULTAR REGISTRO';
            DMDados.tblTabela.Locate(
               DMDados._TIPO_CAMPO,
               _INSUPD_ID, [loCaseInsensitive, loPartialKey]);
         end;
   end;
end;

end.

